// pages/note/note.js
let http = require("../../utils/http.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    "notesList":[],
    "total":"",
    "currPage":1
  },
  toNote: function (res) {
    // 获取用户信息
    wx.getSetting({
      success(re) {
        // console.log("res", res)
        if (re.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.login({
            success: function (e) {
              wx.setStorageSync('nickName', res.detail.userInfo.nickName)
              wx.setStorageSync('avatarUrl', res.detail.userInfo.avatarUrl)
              wx.setStorageSync('code', e.code)
              wx.navigateTo({
                url: 'leaveNotes/leaveNotes'
              })
            },
            fail(res) {
              console.log("获取用户信息失败", res)
            }
          })
        } else {
          wx.showModal({
            title: '您未允许头像、昵称信息授权',
            content: '请点击确定',
            success: res => {
              if (res.confirm) {
                wx.openSetting()
              }
            }
          })
        }
      },
    })
  },
  getList: function () {
    let that = this;
    http.post('wedding/getNotesList',{"page":that.data.currPage},{ 'content-type': 'application/json' }).then(res => {
      let list = res.list;
      let notesList = that.data.notesList.concat(list);
      that.setData({
        "notesList":notesList,
        "total":res.total
        });
    }, 0)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("上滑加载下一页");
    let currPage = this.data.currPage;
    let len = this.data.notesList.length;
    let total = this.data.total;
    currPage++;
    console.log("我是总条数"+total);
    console.log("我是目前条数"+len);
    if(total > len){
      this.setData({"currPage":currPage});
      this.getList()
    }else{
      wx.showToast({
        title: '已经是最后一页啦',
        icon: 'none',    //如果要纯文本，不要icon，将值设为'none'
        duration: 2000     
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      "notesList":[],
      "currPage":1
    })
    this.getList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})